<template>
	<view>
		<!-- 主体表单 -->

		<view class="main1">
			<view class="back-btn yticon icon-zuojiantou-up" @click="navBack"></view>
			<view class="title_des">智慧扫扫</view>
			<view class="item">
				<image class="login_image" src="../../../static/login/userName.png"></image>
				<input v-model="companyId" class="main-input" placeholder="请输入公司编号" />
			</view>
			<view class="item">
				<image class="login_image" src="../../../static/login/userName.png"></image>
				<input v-model="empId" class="main-input" placeholder="请输入账号" />
			</view>
			<view style="margin-top: 8px"></view>
			<view class="item">
				<image class="login_image" src="../../../static/login/password.png"></image>
				<input v-model="password" class="main-input" type="password" placeholder="请输入密码" />
			</view>
		</view>
		<view class="btn_login" @click="startLogin">登录</view>
		<!-- 底部信息 -->
		<view class="footer" style="margin-top: 16px">
			<view>
				<navigator url="/pages/my/register/select" open-type="navigate">新用户注册</navigator>
			</view>
			<view style="margin-left: 32%;">
				<navigator url="/pages/my/forget/person" open-type="navigate">忘记密码</navigator>
			</view>
		</view>
	
		<!-- 其他登录 	
		<view class="weixinLogin">
			<view style="border: 1upx inset;width:30%;float: left;margin-top: 2%;margin-left: 3%;border-color: #F8F8F8;"></view>
			第三方账号登录
			<view style="border: 1upx inset;width:30%;float: right;margin-top: 2%;margin-right: 3%;border-color: #F8F8F8;"></view>
		</view>
		<view class="other_login cuIcon">
			<view class="login_icon_wx">
				<image style="width: 50px;height: 50px;" src="../../../static/wx.png" @tap="login_weixin"></image>
			</view>
		</view>-->
	
	</view>
</template>

<script>
	import md5 from '@/common/md5.js'

	var _this;
	
	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				companyId: '', //用户/电话
				empId: '', //用户/电话
				password: '', //密码
				isRotate: false //是否加载旋转
			};
		},

		mounted() {
			_this = this;
			var userinfo=this.$queue.getData('userinfo');
			if(userinfo){
				this.companyId=userinfo.companyId;
				this.empId=userinfo.empId;
			}
			
		},
		destroyed() {
			uni.hideKeyboard();
		},
		onShow: function() {
			// this.$queue.setData('token',"");
			console.log('login Show');
			this.$Request
					.getT('/user/login', {
						user_name: "AA",
						password: "AA",
						db_name: 'kingzhizhu'
					})
					.then(res => {
						if (res.status == 0) {
							this.$queue.setData('token', res.token);
						} else {
							this.$queue.showToast(res.message);
						}
					})
					.catch(res => {
             });
			
		},
		methods: {
			navBack() {
				uni.navigateBack();
			},
			toLogin() {
				this.$queue.showLoading('登录中...');
				this.$Request
					.post('/db/storedproc', {
						proc_name:"PSC_APPLogin",
						method :"open_proc",
						db_name: 'kingzhizhu',
						params: [{
						    "name": "@companyId",
						    "value": _this.companyId
						  }, {
						    "name": "@empId",
						    "value": _this.empId
						  }]
					}).then(res => {
						uni.hideLoading();
						if(res.status==0){
						   if(res.data.length==0){
							   this.$queue.showToast("公司编码不存在");
							   return;
						   }
							var empId =res.data[0][1];
							if(empId==""){
								this.$queue.showToast("账号不存在");
								return;
							}
							var password=res.data[0][3];
							console.log("password--",_this.password);
							var md5password=md5.hex_md5(_this.password);
							console.log("md5password",md5password);
						
							this.$queue.setData('isLogin',true);
							var userinfo={
								empId:_this.empId,
								companyId:_this.companyId
							}
							this.$queue.setData('userinfo',userinfo);
							
							uni.switchTab({
								url: '/pages/tabBar/component/component'
							  }); 
							
						/*	if(password==md5password){
								this.$queue.setData('isLogin',true);
								uni.switchTab({
									url: '/pages/tabBar/component/component'
								  }); 
							}else{
								this.$queue.showToast("密码错误");
							}*/
						}
					}).catch(res => {
						uni.hideLoading();
					});
					
			},
		
			getUserInfo() {
				this.$Request
					.getT('/system/user/token')
					.then(res => {
						if (res.resultCode == 1) {
							this.$queue.setData('password', this.passData);
							this.$queue.setData('type', res.data.type);
							this.$queue.setData('id', res.data.id);
							if (res.data.type == 1) {
								this.$queue.setData('userName', res.data.telNumber);
								this.$queue.setData('mobile', res.data.telNumber);
							} else {
								this.$queue.setData('userName', res.data.usrMail);
								this.$queue.setData('mobile', res.data.usrMail);
							}
							this.$queue.setData('nickName', res.data.userName);
							this.loginJifen();

							// uni.switchTab({
							// 	url: '/pages/tabbar/home/home'
							// })
							uni.navigateBack({});
						} else {
							this.$queue.showToast(res.errmsg);
						}
						uni.hideLoading();
					})
					.catch(res => {
						uni.hideLoading();
					});
			},
			checkOpenid(openid) {
				this.$queue.showLoading('登录中...');
				this.$queue.setData('openid', openid);
				this.$Request
					.postF('/system/user/openId', {
						openId: openid
					})
					.then(res => {
						if (res.errmsg == '微信号未绑定') {
							uni.navigateTo({
								url: '/pages/my/bind/bind'
							});
						} else {
							this.$queue.setData('token', res.data);
							this.getUserInfo();
						}
						uni.hideLoading();
					})
					.catch(res => {
						uni.hideLoading();
					});
			},
			startLogin() {
				if (this.companyId.length == 0) {
					this.$queue.showToast('请输入公司编码');
					return;
				}
				if (this.empId.length == 0) {
					this.$queue.showToast('请输入账号');
					return;
				}
				if (this.password.length < 1) {
					this.$queue.showToast('请输入密码');
					return;
				}
				this.toLogin();
			},
			loginJifen() {
				this.$Request
					.postF('/basics/scoreDetail/save', {
						scoreType: 0,
						score: 2
					})
					.then(res => {})
					.catch(res => {
						uni.hideLoading();
					});
			},
			login_weixin() {
				let that = this;
				// uni.login({
				// 	provider: 'weixin',
				// 	success: function(loginRes) {
				// 		console.log(loginRes.authResult);
				// 		that.checkOpenid(loginRes.authResult.openid);

				// 	}
				// });
				this.checkOpenid('123456');
			}
		}
	};
</script>

<style>
	@import url('../../../static/css/main.css');
	@import url('../../../static/css/icon.css');
	.type {
		display: flex;
		margin-left: 16px;
		border-bottom: 1px solid #eeeeee;
		width: 100%;
	}

	.title_des {
		font-weight: bold;
		color: #0055b8;
		font-size: 22px;
		margin-bottom: 32px;
		text-align: center;
	}

	.login_icon_wx {
		color: #999999;
		border: none;
	}

	.weixinLogin {
		color: #999999;
		text-align: center;
		font-size: 12px;
		margin-top: 60px;
		left: auto;
		right: auto;
	}

	.footer_des {
		color: #666666;
		text-align: center;
		font-size: 14px;
		margin-top: 40px;
	}

	.getCode {
		font-size: 14px;
		margin-left: 40px;
		color: #0055b8;
	}

	.item {
		line-height: 40px;
		display: flex;
	}

	.login_image {
		margin-top: 8px;
		width: 20px;
		height: 25px;
	}

	.main-input {
		font-size: 14px;
		margin-left: 16px;
		border-bottom: 1px solid #eeeeee;
		height: 40px;
		line-height: 40px;
	}

	.main-input-code {
		width: 70%;
		font-size: 14px;
		height: 40px;
		line-height: 40px;
	}

	.btn_login {
		color: #ffffff;
		font-size: 16px;
		width: 260px;
		height: 40px;
		background: #0055b8;
		border-radius: 8px;
		line-height: 40px;
		text-align: center;
		margin-left: auto;
		margin-right: auto;
		margin-top: 45px;
	}

	.main-input {
		flex: 1;
		text-align: left;
		font-size: 28 upx;
		padding-right: 10 upx;
		margin-left: 20 upx;
	}
</style>
